package p.ithorns.framework.common.model;

import p.ithorns.framework.common.utils.Assert;
import p.ithorns.framework.core.constants.FileConsts;
import p.ithorns.framework.common.utils.FileUtil;

import java.io.File;
import java.net.URI;
import java.nio.file.Path;

/**
 * FileWrapper
 * 文件信息
 *
 * @author Ht.L
 * @date 2023/6/3 13:47
 * @since 1.0.0
 */
public class Asset {

    /**
     * 资源名称
     * 如果是文件夹，就是文件夹名称
     * 如果是文件，就是文件名称
     */
    private final String name;

    /**
     * 相对资源路径
     * 项目根路径开始
     * 例 /wls/files/map-layer/...
     */
    private final URI baseUri;

    /**
     * 绝对路径
     * 从盘符开始的物理地址
     * 例：D:/locate-engine/wls/files/map-layer/...
     */
    private final Path absPath;

    /**
     * 是否文件目录
     */
    private boolean isDirectory;


    public Asset(String name, String base) {
        this(name, base, false);
    }


    public Asset(String name, String base, boolean isDirectory) {
        this.name = name;
        this.isDirectory = isDirectory;
        if (isDirectory) {
            this.baseUri = setBaseUri(base + FileConsts.PATH_SYMBOL + name);
            this.absPath = FileUtil.getAbsPathOfProject(base + File.separator + name);
        } else {
            this.baseUri = setBaseUri(base);
            this.absPath = FileUtil.getAbsPathOfProject(base);
        }
    }

    public Asset(String name, URI base, Path abs) {
        this.name = name;
        this.baseUri = base;
        this.absPath = abs;
    }


    /**
     * 获取文件绝对路径
     * 仅针对文件
     */
    public String getFileAbsPath() {
        if (isDirectory) {
            return absPath.toString();
        }
        return appendFileToAbsPath(name);
    }


    /**
     * 拼接文件绝对路径
     */
    public String appendFileToAbsPath(String fileName) {
        Assert.hasText(fileName, "File name is null");
        return null != absPath ? absPath + File.separator + fileName : fileName;
    }

    /**
     * 获取文件相对路径
     */
    public String getFileRelatePath() {
        if (isDirectory) {
            return this.baseUri.toString();
        }
        return appendFileToRelatePath(name);
    }


    /**
     * 拼接文件相对路径
     */
    public String appendFileToRelatePath(String fileName) {
        Assert.hasText(fileName, "File name is null");
        return null != baseUri ? baseUri + FileConsts.PATH_SYMBOL + fileName : fileName;
    }

    /**
     * 获取相对地址(URI)
     *
     * @param relate -相对基础路径
     * @return URI
     */
    private URI setBaseUri(String relate) {
        assert null != relate;
        return URI.create(relate);
    }

    @Override
    public String toString() {
        return "{" +
                "name:'" + name + '\'' +
                ", baseUri:'" + baseUri + '\'' +
                ", absPath:'" + absPath + '\'' +
                ", isDir:" + isDirectory +
                "}";
    }


    public URI getBaseUri() {
        return baseUri;
    }

    public String getName() {
        return name;
    }

    public Path getAbsPath() {
        return absPath;
    }
}